package com.sann.vod.controller;

import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.sann.common.utils.R;
import com.sann.vod.service.VodService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/eduvod/video")
@CrossOrigin
@Slf4j
public class VodController {

    @Autowired
    private VodService vodService;

    @ApiOperation(value = "视频上传到阿里云")
    @PostMapping(value = "/uploadAliVideoStream")
    public R uploadAliVideoStream(MultipartFile file){
        String videoId = this.vodService.uploadAliVideoStream(file);
        return R.ok().data("videoId", videoId);
    }

    @ApiOperation(value = "根据视频id删除阿里云视频")
    @DeleteMapping(value = "/deleteAliVideo/{videoId}")
    public R deleteAliVideo(@PathVariable String videoId){
        try {
//            Thread.sleep(10000);
            DeleteVideoResponse response = this.vodService.deleteAliVideo(videoId);
            log.info("RequestId: {}", response.getRequestId());
            return R.ok();
        } catch (Exception e) {
            log.error("删除视频错误信息：{}", e.getLocalizedMessage());
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "批量删除阿里云视频")
    @DeleteMapping(value = "/batchDeleteVideo")
    public R batchDeleteVideo(@ApiParam(name = "videoIdList", value = "云端视频Id集合", required = true)
            @RequestParam List<String> videoIdList){
        try {
            this.vodService.batchDeleteVideo(videoIdList);
        }catch (Exception e){
            log.error("批量删除视频错误信息：{}", e.getLocalizedMessage());
            e.printStackTrace();
            return R.error();
        }
        return R.ok();
    }
}
